// ()() (())

function gen (n: number): Array<any> {
  let result: Array<any> = []

  if (n == 0) {
    return [""]
  }

  for (let i = 0; i < n; i++) {
    let a = gen(i)
    let b = gen(n - i - 1)
    for (let aStr of a) {
      for (let bStr of b) {
        result.push("(" + aStr + ")" + bStr)
      }
    }
  }
  

  return result
}

console.log(gen(3))  
// ["((()))","(()())","(())()","()(())","()()()"]